{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# JavaScript 对象\n",
    "JavaScript 中的所有事物都是对象：字符串、数值、数组、函数...\n",
    "\n",
    "此外，JavaScript 允许自定义对象。\n",
    "\n",
    "对象只是一种特殊的数据。对象拥有**属性**和**方法**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var message = \"hello anlzou!\";\n",
    "\n",
    "// 访问对象的属性\n",
    "var x = message.length;\n",
    "x;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO ANLZOU!'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var a = message.toUpperCase();\n",
    "a;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO ANLZOU!'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// 访问对象的方法,toLocaleUpperCase()为针对特定地区的实现转换\n",
    "var y = message.toLocaleUpperCase();\n",
    "y;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建 JavaScript 对象\n",
    "JavaScript 对象就是一个 **name:value** 集合。\n",
    "\n",
    "创建新对象有两种不同的方法：\n",
    "- 使用 Object 定义并创建对象的实例。\n",
    "- 使用函数来定义对象，然后创建新的对象实例。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用 Object\n",
    "\n",
    "在 JvaScript 中，几乎所有的对象都是 Object 类型的实例，它们都会从 Objet.prototype 继承属性和方法。\n",
    "\n",
    "Object 构造函数创建一个对象包装器。\n",
    "\n",
    "Object 构造函数，会根据给定的参数创建对象，具体有以下情况：\n",
    "- 如果给定值是 null 或 undefined，将会创建并返回一个空对象。\n",
    "- 如果传进去的是一个基本类型的值，则会构造其包装类型的对象。\n",
    "- 如果传进去的是引用类型的值，仍然会返回这个值，经他们复制的变量保有和源对象相同的引用地址。\n",
    "- 当以非构造函数形式被调用时，Object 的行为等同于 new Object()。\n",
    "\n",
    "语法格式：\n",
    "```js\n",
    "// 以构造函数形式来调用\n",
    "new Object([value])\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'blue'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person=new Object();\n",
    "person.firstname=\"John\";\n",
    "person.lastname=\"Doe\";\n",
    "person.age=50;\n",
    "person.eyecolor=\"blue\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以使用对象字面量来创建对象，语法格式如下：\n",
    "```js\n",
    "{ name1 : value1, name2 : value2,...nameN : valueN }\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ firstname: 'John', lastname: 'Doe', age: 50, eyecolor: 'blue' }"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person={firstname:\"John\",lastname:\"Doe\",age:50,eyecolor:\"blue\"};"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用对象构造器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "function person(firstname,lastname,age,eyecolor){\n",
    "    this.firstname=firstname;\n",
    "    this.lastname=lastname;\n",
    "    this.age=age;\n",
    "    this.eyecolor=eyecolor;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "var myFather = new person(\"Joho\",\"Doe\",30,\"black\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myFather.age;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 把属性添加到 JavaScript 对象\n",
    "可以通过为对象赋值，向已有对象添加新属性："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'software engineering'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person.work = \"software engineering\";\n",
    "\n",
    "x = person.work;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 把方法添加到 JavaScript 对象\n",
    "方法只不过是附加在对象上的函数。\n",
    "\n",
    "在构造器函数内部定义对象的方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "function person(firstname,lastname,age,eyecolor){\n",
    "    this.firstname=firstname;\n",
    "    this.lastname=lastname;\n",
    "    this.age=age;\n",
    "    this.eyecolor=eyecolor;\n",
    "\n",
    "    this.changeName=changeName;\n",
    "    function changeName(name){\n",
    "        this.lastname=name;\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Ben'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myFather = new person(\"Joho\",\"Doe\",30,\"black\");\n",
    "myFather.changeName(\"Ben\");\n",
    "\n",
    "myFather.lastname;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JavaScript for...in 循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JohnDoe25\n"
     ]
    }
   ],
   "source": [
    "var person={fname:\"John\",lname:\"Doe\",age:25}; \n",
    "\n",
    "(()=>{\n",
    "    var txt = \"\"\n",
    "\n",
    "    for (x in person){\n",
    "        txt = txt + person[x];\n",
    "    }\n",
    "    console.log(txt);\n",
    "})();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 补充：JavaScript 没有类\n",
    "JavaScript 是面向对象的语言，但 JavaScript 不使用类。\n",
    "\n",
    "在 JavaScript 中，不会创建类，也不会通过类来创建对象（就像在其他面向对象的语言中那样）。\n",
    "\n",
    "JavaScript 基于 prototype (原型)，而不是基于类的。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Javascript (Node.js)",
   "language": "javascript",
   "name": "javascript"
  },
  "language_info": {
   "file_extension": ".js",
   "mimetype": "application/javascript",
   "name": "javascript",
   "version": "13.13.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
